package com.smartguard.util;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.websocket.Session;

import com.alibaba.fastjson.JSON;
import com.smartguard.bean.ClientEvent;
import com.smartguard.bean.Device;
import com.smartguard.common.Const;
import com.smartguard.service.DeviceService;
import com.smartguard.websocket.SGWebSocket;

/**
 * WebSocket 发送信息 消息格式 { "deviceHash" : deviceHash, "eventType" : eventType }
 * eventType参考doc/rule.txt
 */
public class MessageUtil {

	/**
	 * 设备感应到震动后发送消息给所属用户的所有设备 <br>
	 * 发送的消息为{"deviceHash" : deviceHash, "eventType" : 1} <br>
	 * 设备接收到消息后判断是否有监控功能，有则开启监控 <br>
	 * 相应的设备发出恐吓
	 * 
	 * @param deviceHash 
	 * @param message
	 * @return
	 */
	public static boolean sendMessageForSensor(String deviceHash) {
		String message = "{'deviceHash' : '" +deviceHash + "', 'eventType' : " + Const.EVENT_SENSOR + "}";
		ClientEvent clientEvent = new ClientEvent();
		clientEvent.setDeviceHash(deviceHash);
		clientEvent.setEventType(Const.EVENT_SENSOR);
		message = JSON.toJSONString(clientEvent);
		System.out.println("test for message string" + message);
		
		
		System.out.println("test send sensor message : " + message);
		return sendMessageToSameUserDevices(deviceHash, message);
	}

	/**
	 * 用户接收到警报后回复 <br>
	 * 发送的消息为{"deviceHash" : deviceHash, "eventType" : 7}
	 * 
	 * @param fromDeviceHash
	 *            为源设备， toDeviceHash 目标设备
	 * @param message
	 * @return
	 */
	public static boolean sendMessageKnowed(String fromDeviceHash, String toDeviceHash) {
		String message = "{" + "'deviceHash' : fromDeviceHash ," + "'eventType' : 7" + "}";
		return sendMessageToOneDevice(toDeviceHash, message);
	}

	/**
	 * 根据deviceHash给设备发送消息
	 * 
	 * @param deviceHash
	 * @param message
	 * @return
	 */
	public static boolean sendMessageToOneDevice(String deviceHash, String message) {
		Map<String, Session> sessions = SGWebSocket.getSessions();
		Session session = sessions.get(deviceHash);

		boolean result = true;
		if (session == null) {
			result = false;
			System.out.println("session is null");
			return result;
			// 某设备没有通过websocket链接到服务器。
		}

		try {
			session.getBasicRemote().sendText(message);
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}

		return result;
	}

	/**
	 * 发送消息给所属用户的所有设备
	 * 
	 * @param deviceHash
	 * @param message
	 * @return
	 */
	public static boolean sendMessageToSameUserDevices(String deviceHash, String message) {

		Map<String, Session> sessions = SGWebSocket.getSessions();

		DeviceService deviceService = new DeviceService();
		List<Device> devices = deviceService.findDeviceOfSameUser(deviceHash);
		Session session;
		boolean result = true;
		for (Device device : devices) {
			session = sessions.get(device.getDeviceHash());
			if (session == null) {
				result = false;
				// 某设备没有通过websocket链接到服务器。
				//TODO
				continue;
			}
			try {
				session.getBasicRemote().sendText(message);
			} catch (IOException e) {
				e.printStackTrace();
				result = false;
			}
		}

		return result;
	}

	
	/**
	 * 发送警告消息给钥匙端
	 * @param message
	 * @return
	 */
	public static boolean sendWarningMessageToKey(String message){
		
		// TODO 还没有确定那个是钥匙端
		return false;
	}

	/**
	 * 测试信息发送
	 */
	public static boolean sendNessageTest() {
		String deviceHash = "d17dc5fe-b25b-37c1-6d4d-5d72d9fc0a61";
		String message = "{" + "'deviceHash' : "+deviceHash+" ," + "'eventType' : 7" + "}";
		ClientEvent clientEvent = new ClientEvent();
		clientEvent.setDeviceHash(deviceHash);
		clientEvent.setEventType(7);
		
		System.out.println(message + " sendmessage");
		message = JSON.toJSONString(clientEvent);
		System.out.println(message + " jsonmessage");
		boolean result = sendMessageToOneDevice(deviceHash, message);
		System.out.println("send result:" + result);
		return result;
	}
}
